<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="Ian Tang">


    <meta name="subtitle" content="岁月不居时节如流">


    <meta name="description" content="好记性不如烂笔头">



<title>Vscode编辑C项目 | 随笔</title>



    <link rel="icon" href="/image/The_Lion_King_32_32.png">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 6.3.0"></head>

<body>
    <script>
        // this function is used to check current theme before page loaded.
        (() => {
            const currentTheme = window.localStorage && window.localStorage.getItem('theme') || '';
            const isDark = currentTheme === 'dark';
            const pagebody = document.getElementsByTagName('body')[0]
            if (isDark) {
                pagebody.classList.add('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Dark"
            } else {
                pagebody.classList.remove('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Light"
            }
        })();
    </script>

    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">Tangkaifei&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">Tangkaifei&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
            <div class="main">
                <div class="container">
    
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Vscode编辑C项目</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">Ian Tang</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">十一月 27, 2022&nbsp;&nbsp;13:17:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/%E6%96%B9%E6%B3%95%EF%BC%9A%E6%96%B9%E6%B3%95%E8%AE%BA%EF%BC%88%E7%BB%8F%E9%AA%8C%E8%B0%88%EF%BC%89/">方法：方法论（经验谈）</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <p>vscode是一个文本编辑器，附带的各种各样的插件很丰富。提前使用MinGW安装好GNU的环境，主要是c和c++编译器，使用vscode进行c语言的编辑，最终组成一个很方便的ide。</p>
<blockquote>
<p>最终效果：实时显示编译阶段的错误、代码片段、补全、格式化、单文件的编译与调试。</p>
</blockquote>
<ul>
<li><p>安装c&#x2F;c++、Runner这两个插件，目的是调用MinGW的编译器。</p>
</li>
<li><p>创建文件夹，在.vscode里创建laungh.json、tasks.json两个文件。laungh.json用于调试；tasks.json用于编译。如果不需要调试可以只创建tasks.json。这两个文件就是为了替代在命令行输入gcc -o xxx xxx.c等这些指令。</p>
</li>
<li><p>因此创建launch.json文件可以在左侧第四个点击Run，选择C++ （GDB&#x2F;LLDB）这个选项</p>
<blockquote>
<p>里面主要修改</p>
<p>“program”: “${workspaceFolder}&#x2F;${fileDirname}&#x2F;${fileBasenameNoExtension}.exe”,</p>
<p>运行的程序是当前工作空间下当前文件目录的exe文件</p>
<p>“miDebuggerPath”: “c:&#x2F;MinGW&#x2F;bin&#x2F;gdb.exe”</p>
<p>运行的调试gdb的目录</p>
</blockquote>
</li>
<li><p>创建tasks.json文件可以使用 terminal-&gt;configuration task里寻找模版，如果没有手动设置</p>
<blockquote>
<p>里面主要设置</p>
<p>“type”: “shell”,</p>
<p>使用shell程序</p>
<p>“command”: “c:&#x2F;windows&#x2F;mingw&#x2F;bin&#x2F;gcc”,</p>
<p>gcc所在的目录</p>
<p>“args”: [</p>
<p>“-g”,</p>
<p> “${file}”,</p>
<p> “-o”,</p>
<p> “${fileDirname}&#x2F;${fileBasenameNoExtension}”</p>
<p> ],</p>
<p>命令行的参数</p>
</blockquote>
</li>
</ul>
<p>有一个自动部署调试c++的工具<strong>vscch</strong>，很好用</p>
<blockquote>
<p>关于c的一些tips：</p>
<p>函数指针：本质上还是一个指针，只不过指向函数的入口地址。函数指针有两个用途：调用函数和做函数的<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%8F%82%E6%95%B0/5934974">参数</a>。</p>
<p>返回值类型 ( * <a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F">指针变量</a>名) ([<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%BD%A2%E5%8F%82">形参</a>列表]);</p>
<p>( * <a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F">指针变量</a>名)一定要有括号，不然就变成返回一个指针的函数类型了。</p>
<p>int * func(int a ); &#x2F;&#x2F;函数</p>
<p>Int (* f)(int a ); &#x2F;&#x2F;函数指针</p>
<p>f &#x3D; func; 函数指针指向func的入口地址。</p>
</blockquote>
<p>先创建task.json，告诉vscode怎么build（compiler）程序，这将创建可执行程序。</p>
<p>创建launch.json，是启动调试器gdb调试。</p>
<p>步骤是先写好c文件，在c文件置于活动页面下，创建task.json，再创建launch.json，这样默认的配置都补全了。</p>
<p>如果需要改标准库路径和版本等信息的话创建.vscode\c_cpp_properties.json</p>
<p>You only need to add to the <strong>Include path</strong> array setting if your program includes header files that are not in your workspace or in the standard library path.&#x2F;如果导入的库既不在你的workspace里也不在标准库里，则需要在include path里加入这个路径。</p>
<p>遇到的问题<br>1、include path用上文提到的vscch工具生成的路径是，不生效，需要改成{}<br>2、如果使用工具配置的时候选的是c++，就不能定义文件名后缀为.c</p>
<hr>
<p>${workspaceFolder} :表示当前workspace文件夹路径，也即C:\Users\admin\Desktop\test</p>
<p>${workspaceRootFolderName}:表示workspace的文件夹名，也即test</p>
<p>${file}:文件自身的绝对路径，也即C:\Users\admin\Desktop\test.vscode\launch.json</p>
<p>${relativeFile}:文件在workspace中的路径，也即.vscode\launch.json</p>
<p>${fileBasenameNoExtension}:当前文件的文件名，不带后缀，也即launch</p>
<p>${fileBasename}:当前文件的文件名，launch.json</p>
<p>${fileDirname}:文件所在的文件夹路径，也即C:\Users\admin\Desktop\test.vscode</p>
<p>${fileExtname}:当前文件的后缀，也即.json</p>
<p>${lineNumber}:当前文件光标所在的行号</p>
<p>${env:PATH}:系统中的环境变量</p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>Ian Tang</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://tkhfree.github.io/2022/11/27/Vscode%E7%BC%96%E8%BE%91C%E9%A1%B9%E7%9B%AE-2022-11-27/">https://tkhfree.github.io/2022/11/27/Vscode%E7%BC%96%E8%BE%91C%E9%A1%B9%E7%9B%AE-2022-11-27/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>Copyright (c) 2019 <a target="_blank" rel="noopener" href="http://creativecommons.org/licenses/by-nc/4.0/">CC-BY-NC-4.0</a> LICENSE</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Do you believe in <strong>DESTINY</strong>?</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/c-c/"># c/c++</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2022/11/27/%E7%BD%91%E7%BB%9C%E5%B9%B3%E5%8F%B0%E5%BC%80%E5%8F%91%E5%A5%97%E4%BB%B6DPDK-2022-11-27/"></a>
            
            
            <a class="next" rel="next" href="/2022/11/27/python%E6%A6%82%E8%BF%B0%E5%92%8C%E6%80%BB%E7%BB%93-2022-11-27/">python概述和总结</a>
            
        </section>


    </article>
</div>

            </div>
            <footer id="footer" class="footer">
    <div class="copyright">
        <span>© Ian Tang | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>

</html>